import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import java.util.ArrayList;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

public class UnoPlayers_UI extends JDialog
{

	private static final long serialVersionUID = -7599127017593377980L;

	private static ArrayList<Player> players;

	private static JPanel panelContent;
	private static JPanel panelButtons;
	private static JButton btnOK;
	private static JButton btnCancel;

	private static JLabel lblTitlePlayers;
	private static JLabel lblTitlePasswords;

	private static JTextField txtP1Name;
	private static JTextField txtP2Name;
	private static JTextField txtP3Name;
	private static JTextField txtP4Name;
	private static JTextField txtP5Name;

	private static JPasswordField txtP1Password;
	private static JPasswordField txtP2Password;
	private static JPasswordField txtP3Password;
	private static JPasswordField txtP4Password;
	private static JPasswordField txtP5Password;

	private static JLabel lblP1;
	private static JLabel lblP2;
	private static JLabel lblP3;
	private static JLabel lblP4;
	private static JLabel lblP5;

	/**
	 * Launch UnoPlayers_UI
	 */
	public static void launch()
	{
		try
		{
			UnoPlayers_UI dialog = new UnoPlayers_UI();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UnoPlayers_UI()
	{
		super();

		players = new ArrayList<Player>();

		panelContent = new JPanel();

		Uno_UI.setAttributesDialog(this, "New Game: Players");
		Uno_UI.setUIManager();

		createPanelContent();
		addWindowListenerClosing();
		createLabels();
		createTextFields();
		createButtons();

		/**
		 * this method gets the number of players selected in the UnoNew_UI
		 * Class
		 */
		toggleTextFieldsBasedOnNumPlayers(UnoNew_UI.getNumPlayer());
	}

	private void createPanelContent()
	{
		getContentPane().setLayout(new BorderLayout());
		panelContent.setForeground(Uno_UI.WHITE);
		panelContent.setBackground(Uno_UI.BLACK);
		panelContent.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panelContent, BorderLayout.CENTER);
		panelContent.setLayout(null);
	}

	private void createButtons()
	{
		panelButtons = new JPanel();
		panelButtons.setBackground(Uno_UI.BLACK);
		panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(panelButtons, BorderLayout.SOUTH);

		createButtonOK();
		createButtonCancel();
	}

	private void createButtonOK()
	{
		btnOK = new JButton("OK");
		addActionListenerButtonOK();
		btnOK.setActionCommand("OK");
		panelButtons.add(btnOK);
		getRootPane().setDefaultButton(btnOK);
	}

	private void addActionListenerButtonOK()
	{
		btnOK.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent okClicked)
			{
				setVisible(false);
				/**
				 * holdPlayer is a method that adds the players to a list
				 */
				addPlayers(UnoNew_UI.getNumPlayer());

				/**
				 * players is a list of players This is how the players are
				 * added to the Uno class.
				 * 
				 * (tried to refactor but ran into scope issues)
				 */
				Uno.addPlayerToGame(players);

				/**
				 * this is a method that simply prints the linked list in the
				 * Uno class
				 * 
				 * i made this just to verify the list was added
				 */
				Uno.startGame();
			}
		});
	}

	private void createButtonCancel()
	{
		btnCancel = new JButton("Cancel");
		addActionListenerButtonCancel();
		btnCancel.setActionCommand("Cancel");
		panelButtons.add(btnCancel);
	}

	private void addActionListenerButtonCancel()
	{
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent cancelClicked)
			{
				clickCloseOrCancel();
			}
		});
	}

	private void createLabels()
	{
		lblP1 = new JLabel("Player 1");
		lblP1.setBounds(10, 54, 71, 14);
		panelContent.add(lblP1);

		lblP2 = new JLabel("Player 2");
		lblP2.setBounds(10, 85, 71, 14);
		panelContent.add(lblP2);

		lblP3 = new JLabel("Player 3");
		lblP3.setBounds(10, 116, 71, 14);
		panelContent.add(lblP3);

		lblP4 = new JLabel("Player 4");
		lblP4.setBounds(10, 147, 71, 14);
		panelContent.add(lblP4);

		lblP5 = new JLabel("Player 5");
		lblP5.setBounds(10, 178, 71, 14);
		panelContent.add(lblP5);
	}

	private void createTextFields()
	{
		createTextFieldsPlayerNames();
		createTextFieldsPlayerPasswords();
	}

	private void createTextFieldsPlayerNames()
	{
		lblTitlePlayers = new JLabel("Name");
		lblTitlePlayers.setFont(Uno_UI.CONSOLAS18);
		lblTitlePlayers.setBounds(88, 11, 163, 29);
		panelContent.add(lblTitlePlayers);
		txtP1Name = new JTextField();
		txtP1Name.setBounds(88, 51, 163, 20);
		txtP1Name.setColumns(10);
		panelContent.add(txtP1Name);

		txtP2Name = new JTextField();
		txtP2Name.setColumns(10);
		txtP2Name.setBounds(88, 82, 163, 20);
		panelContent.add(txtP2Name);

		txtP3Name = new JTextField();
		txtP3Name.setColumns(10);
		txtP3Name.setBounds(88, 113, 163, 20);
		panelContent.add(txtP3Name);

		txtP4Name = new JTextField();
		txtP4Name.setColumns(10);
		txtP4Name.setBounds(88, 144, 163, 20);
		panelContent.add(txtP4Name);

		txtP5Name = new JTextField();
		txtP5Name.setColumns(10);
		txtP5Name.setBounds(88, 175, 163, 20);
		panelContent.add(txtP5Name);
	}

	private void createTextFieldsPlayerPasswords()
	{
		lblTitlePasswords = new JLabel("Password");
		lblTitlePasswords.setFont(Uno_UI.CONSOLAS18);
		lblTitlePasswords.setBounds(261, 11, 163, 29);
		panelContent.add(lblTitlePasswords);
		txtP1Password = new JPasswordField();
		txtP1Password.setBounds(261, 51, 163, 20);
		panelContent.add(txtP1Password);

		txtP2Password = new JPasswordField();
		txtP2Password.setBounds(261, 82, 163, 20);
		panelContent.add(txtP2Password);

		txtP3Password = new JPasswordField();
		txtP3Password.setBounds(261, 115, 163, 20);
		panelContent.add(txtP3Password);

		txtP4Password = new JPasswordField();
		txtP4Password.setBounds(261, 144, 163, 20);
		panelContent.add(txtP4Password);

		txtP5Password = new JPasswordField();
		txtP5Password.setBounds(261, 175, 163, 20);
		panelContent.add(txtP5Password);
	}

	/**
	 * this method holds the players in a list. Uno can then ask Uno_players for
	 * the list to store in the linked list
	 * 
	 * @param numberOfPlayers
	 */

	public void addPlayers(int numberOfPlayers)
	{
		/**
		 * adds two player objects to players list players list can be found at
		 * the top of the class...
		 */

		players.add(newPlayer(txtP1Name.getText(), txtP1Password.getPassword(),
				1));
		players.add(newPlayer(txtP2Name.getText(), txtP2Password.getPassword(),
				2));

		/**
		 * adds three player objects to players list
		 */
		if (numberOfPlayers > 2)
		{
			players.add(newPlayer(txtP3Name.getText(),
					txtP3Password.getPassword(), 3));
		}

		/**
		 * adds four player objects to players list
		 */

		if (numberOfPlayers > 3)
		{
			players.add(newPlayer(txtP4Name.getText(),
					txtP4Password.getPassword(), 4));
		}

		/**
		 * adds five player objects to players list
		 */
		if (numberOfPlayers > 4)
		{
			players.add(newPlayer(txtP5Name.getText(),
					txtP5Password.getPassword(), 5));
		}
	}

	/**
	 * this method displays the test fields and password fields according to how
	 * many players were chosen
	 * 
	 * This method is insanely huge! Its because i have to create each field and
	 * label in the method so it stays in scope.. Feel free to refactor..
	 * 
	 * @param numberOfPlayers
	 */
	public void toggleTextFieldsBasedOnNumPlayers(int numberOfPlayers)
	{
		lblP3.setVisible(false);
		txtP3Name.setVisible(false);
		txtP3Password.setVisible(false);
		
		lblP4.setVisible(false);
		txtP4Name.setVisible(false);
		txtP4Password.setVisible(false);
		
		lblP5.setVisible(false);
		txtP5Name.setVisible(false);
		txtP5Password.setVisible(false);		

		/**
		 * Sets the first three text fields and password fields to true
		 */
		if (numberOfPlayers > 2)
		{
			lblP3.setVisible(true);
			txtP3Name.setVisible(true);
			txtP3Password.setVisible(true);
		}

		/**
		 * Sets the first four text fields and password fields to true
		 */
		if (numberOfPlayers > 3)
		{
			lblP4.setVisible(true);
			txtP4Name.setVisible(true);
			txtP4Password.setVisible(true);
		}

		/**
		 * Sets the first five text fields and password fields to true
		 */
		if (numberOfPlayers > 5)
		{
			lblP5.setVisible(true);
			txtP5Name.setVisible(true);
			txtP5Password.setVisible(true);
		}

	}

	private void addWindowListenerClosing()
	{
		// confirms exit
		addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent windowClosingEvent)
			{
				clickCloseOrCancel();
			}
		});
	}

	private void clickCloseOrCancel()
	{
		dispose();
		Uno.unoNew();
	}

	/**
	 * new player helper autogenerates player name if null
	 * 
	 * @param name
	 * @param password
	 * @param playerIndex
	 * @return
	 */
	private Player newPlayer(String name, char[] password, int playerIndex)
	{
		if (name.replace(" ", "").length() < 1)
		{
			name = "Player " + playerIndex;
		}
		return new Player(name, password);
	}

}
